<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}用户`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="用户头像" prop="avatar">
        <UploadImg v-model:image-url="drawerProps.row!.url" width="135px" height="135px" :file-size="3">
          <template #empty>
            <el-icon><Avatar /></el-icon>
            <span>请上传头像</span>
          </template>
          <template #tip> 头像大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="用户姓名" prop="name">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写用户姓名" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input v-model="drawerProps.row!.pwd" placeholder="请填写用户密码" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status" >
        <el-select v-model="drawerProps.row!.status" placeholder="请选择状态">
          <el-option label="正常" :value="1"/>
          <el-option label="禁用" :value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item label="角色选择" prop="rolesId" >
        <el-select v-model="drawerProps.row!.rolesId" placeholder="请选择角色" multiple filterable>
          <el-option v-for="item in rolesArr" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="drawerProps.row!.remark" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive,onMounted,computed } from "vue";
import { genderType } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { User } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import UploadImgs from "@/components/Upload/Imgs.vue";
import {reqGetAllRoles} from '@/api/modules/system'

const rules = reactive({
  url: [{ required: true, message: "请上传用户头像" }],
  name: [{ required: true, message: "请填写用户姓名" }],
  pwd: [{ required: true, message: "请填写密码" }],
  status: [{ required: true, message: "请填写状态" }],
  rolesId: [{ required: true, message: "请填写角色" ,trigger: 'change'}],
  remark: [{ required: true, message: "请填写备注" }]
}); 

const rolesArr=ref<any>([])

onMounted(async()=>{
    let result:any=await reqGetAllRoles()
    rolesArr.value=result.data.map((t:any)=>{t.id=t.id.toString(); return t});
})

interface DrawerProps {
  title: string;
  isView: boolean;
  row: any;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  if(params.title!=='新增'){
      drawerProps.value.row.rolesId=drawerProps.value.row.rolesId.split(",")
  }
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
        drawerProps.value.row.rolesId=drawerProps.value.row.rolesId.join(',')
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
